package org.example.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.*;
import org.example.erp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 工艺流程设置
@RestController
@RequestMapping("/api")
public class ProceduresController {
    @Autowired
    ProceduresService proceduresService;
    @Autowired
    ProceduresDetailsService proceduresDetailsService;
    @Autowired
    ProcedureProductService procedureProductService;
    @Autowired
    ProcedureProductDomService procedureProductDomService;

    @GetMapping("/process/find")
    public DataResult find(
            @RequestParam(value = "procedureCode") String procedureCode,
            @RequestParam(value = "procedureName") String procedureName,
            @RequestParam(value = "isEnabled") String isEnabled,
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        Page<Procedures> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Procedures> queryWrapper = new QueryWrapper<>();
        if (procedureCode != null && !procedureCode.isEmpty()) {
            queryWrapper.lambda().like(Procedures::getProcedureCode, procedureCode);
        }
        if (procedureName != null && !procedureName.isEmpty()) {
            queryWrapper.lambda().like(Procedures::getProcedureName, procedureName);
        }
        if (isEnabled != null && !isEnabled.isEmpty()) {
            queryWrapper.lambda().like(Procedures::getIsEnabled, isEnabled);
        }
        queryWrapper.orderByDesc("procedure_code");
        page = proceduresService.page(page, queryWrapper);
        return DataResult.success(page);
    }

    @GetMapping("/process/findById")
    public DataResult findById(@RequestParam String id) {
        return DataResult.success(proceduresService.getById(id));
    }

    @PostMapping("/process/insert")
    public DataResult add(@RequestBody Procedures procedures) {
        boolean save = proceduresService.save(procedures);
        return DataResult.success(save ? "成功" : "失败");
    }

    @DeleteMapping("/process/delete")
    public DataResult delete(@RequestBody List<String> ids) {
        boolean removeBatchByIds = proceduresService.deleteDetails(ids);
        return DataResult.success(removeBatchByIds ? "成功" : "失败");
    }

    @PutMapping("/process/update")
    public DataResult update(@RequestBody Procedures procedures) {
        boolean updateById = proceduresService.updateById(procedures);
        return DataResult.success(updateById ? "成功" : "失败");
    }

    // 工艺流程详情
    @GetMapping("/process/findDetails")
    public DataResult findDetails(@RequestParam String processCode) {
        Procedures list = proceduresService.findDetails(processCode);
        if (list == null || list.getProcedureDetails() == null || list.getProcedureProduct() == null) {
            return DataResult.success(list);
        }
        boolean allNull = true;
        boolean allNull1 = true;

        for (ProcedureDetails p : list.getProcedureDetails()) {
            if (p.getId() != null) {
                allNull = false;
                break;
            }
        }
        for (ProcedureProduct p : list.getProcedureProduct()) {
            boolean isNull=true;
            for (ProcedureProductDom p1 : p.getProcedureProductDom()) {
                if (p1.getId() != null) {
                    isNull = false;
                    break;
                }
            }
            if (isNull) {
                p.setProcedureProductDom(null);
            }
            if (p.getId() != null) {
                allNull1 = false;
                break;
            }
        }

        if (allNull) {
            list.setProcedureDetails(null);
        } else if (allNull1) {
            list.setProcedureProduct(null);
        }
        return DataResult.success(list);
    }

    // 工艺流程详情的工序添加
    @PostMapping("/process/insertDetailsProcess")
    public DataResult addDetails(@RequestBody ProcedureDetails procedureDetails) {
        boolean updateById = proceduresDetailsService.add(procedureDetails);
        return DataResult.success(updateById ? "成功" : "失败");
    }

    // 工艺流程详情的工序修改
    @PutMapping("/process/updateDetailsProcess")
    public DataResult updateDetails(@RequestBody ProcedureDetails procedureDetails) {
        boolean updateById = proceduresDetailsService.updata(procedureDetails);
        return DataResult.success(updateById ? "成功" : "失败");
    }

    // 工艺流程详情的工序删除
    @DeleteMapping("/process/deleteDetailsProcess")
    public DataResult deleteDetails(@RequestBody List<String> ids) {
        boolean updateById = proceduresDetailsService.removeByIds(ids);
        return DataResult.success(updateById ? "成功" : "失败");
    }

    // 查询工艺流程中产品是否唯一
    @GetMapping("/process/findProduct")
    public DataResult findProduct(@RequestParam String productCode, String p) {
        QueryWrapper<ProcedureProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ProcedureProduct::getProductCode, productCode);
        ProcedureProduct list = procedureProductService.getOne(queryWrapper);
        // p为1 时，代表是添加，2代表是修改
        // 当是添加时，只要有值就返回true,否则返回false，当时修改时，查询的值与传入的值不相等就返回true，否则返回false
        if (p.equals("1")) {
            return DataResult.success(list == null);
        } else {
            return DataResult.success(list);
        }
    }

    //添加产品到工艺路线中
    @PostMapping("/process/insertProduct")
    public DataResult addProduct(@RequestBody ProcedureProduct procedureProduct) {
        boolean updateById = procedureProductService.save(procedureProduct);
        return DataResult.success(updateById ? "成功" : "失败");
    }

    // 删除产品
    @DeleteMapping("/process/deleteProduct")
    public DataResult deleteProduct(@RequestBody List<String> ids) {
        boolean updateById = procedureProductService.removeByIds(ids);
        return DataResult.success(updateById ? "成功" : "失败");
    }

    // 修改产品
    @PutMapping("/process/updateProduct")
    public DataResult updateProduct(@RequestBody ProcedureProduct procedureProduct) {
        boolean updateById = procedureProductService.updateById(procedureProduct);
        return DataResult.success(updateById ? "成功" : "失败");
    }

    // 查询产品中有那些组成
    @GetMapping("process/findProductByProductCode")
    public DataResult findProductByProductCode(@RequestParam String procedureCode) {
        QueryWrapper<ProcedureProductDom> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ProcedureProductDom::getProcedureCode, procedureCode);
        List<ProcedureProductDom> list = procedureProductDomService.list();
        return DataResult.success(list);
    }

    // 将组成添加到相应的工艺路线中
    @PostMapping("/process/insertProductDetails")
    public DataResult addProductDetails(@RequestBody ProcedureProductDom procedureProductDom) {
        boolean save = procedureProductDomService.save(procedureProductDom);
        return DataResult.success(save ? "成功" : "失败");
    }

    // 将工艺路线中的组成删除
    @DeleteMapping("/process/deleteProductDetails")
    public DataResult deleteProductDetails(@RequestBody List<String> ids) {
        boolean removeBatchByIds = procedureProductDomService.removeByIds(ids);
        return DataResult.success(removeBatchByIds ? "成功" : "失败");
    }

    // 修改工艺路线中的组成
    @PutMapping("/process/updateProductDetails")
    public DataResult updateProductDetails(@RequestBody ProcedureProductDom procedureProductDom) {
        boolean updateById = procedureProductDomService.updateById(procedureProductDom);
        return DataResult.success(updateById ? "成功" : "失败");
    }
}
